---
title: Asynchronous Operations
---

Memobase supports asynchronous operations for inserting and flushing data. Offloading these tasks to background processes improves your application's performance and responsiveness by preventing memory operations from blocking the main thread.

When you perform an asynchronous insert or flush, the data is queued for processing, and the method returns immediately. This allows your application to continue executing while Memobase handles the data in the background.

### SDK Examples

Here’s how to use both synchronous and asynchronous operations in our SDKs:

<CodeGroup>
```python Python
from memobase import MemoBaseClient
from memobase.core.blob import ChatBlob

client = MemoBaseClient(project_url='YOUR_PROJECT_URL', api_key='YOUR_API_KEY')
user = client.get_user('some_user_id')

# Create a data blob
blob = ChatBlob(messages=[
    {"role": "user", "content": "Hi, I'm here again"},
    {"role": "assistant", "content": "Hi, Gus! How can I help you?"}
])

# Asynchronous insert (default behavior)
blob_id = user.insert(blob)

# Asynchronous flush (default behavior)
user.flush()

# Synchronous flush (waits for completion)
user.flush(sync=True)
```

```javascript JavaScript
import { MemoBaseClient, Blob, BlobType } from '@memobase/memobase';

const client = new MemoBaseClient(process.env.MEMOBASE_PROJECT_URL, process.env.MEMOBASE_API_KEY);
const user = await client.getUser(userId);

// Asynchronous insert
const blobId = await user.insert(Blob.parse({
  type: BlobType.Enum.chat,
  messages: [
    { role: 'user', content: 'Hi, I\'m here again' },
    { role: 'assistant', content: 'Hi, Gus! How can I help you?' }
  ]
}));

// Asynchronous flush
await user.flush(BlobType.Enum.chat);
```

```go Go
import (
    "fmt"
    "log"

    "github.com/memodb-io/memobase/src/client/memobase-go/blob"
    "github.com/memodb-io/memobase/src/client/memobase-go/core"
)

func main() {
    client, err := core.NewMemoBaseClient("YOUR_PROJECT_URL", "YOUR_API_KEY")
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }

    user, err := client.GetUser("EXISTING_USER_ID", false)
    if err != nil {
        log.Fatalf("Failed to get user: %v", err)
    }

    chatBlob := &blob.ChatBlob{
        Messages: []blob.OpenAICompatibleMessage{
            {Role: "user", Content: "Hello, I am Jinjia!"},
            {Role: "assistant", Content: "Hi there! How can I help you today?"},
        },
    }

    // Asynchronous insert (sync=false)
    blobID, err := user.Insert(chatBlob, false)
    if err != nil {
        log.Fatalf("Failed to insert blob: %v", err)
    }
    fmt.Printf("Successfully queued blob for insertion with ID: %s\n", blobID)

    // Asynchronous flush (sync=false)
    err = user.Flush(blob.ChatType, false)
    if err != nil {
        log.Fatalf("Failed to flush buffer: %v", err)
    }
    fmt.Println("Successfully queued buffer for flushing")

    // Synchronous flush (sync=true)
    err = user.Flush(blob.ChatType, true)
    if err != nil {
        log.Fatalf("Failed to flush buffer: %v", err)
    }
    fmt.Println("Successfully flushed buffer synchronously")
}
```
</CodeGroup>

For more details, see the API reference for [flush](/api-reference/buffer/flush) and [insert](/api-reference/blobs/insert_data).
